#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project : chat_model 
# @File    : user_logic.py
# @IDE     : PyCharm 
# @Author  :ZH
# @Time    : 2025/1/17 10:57
__all__ = ['user_logic']

from tortoise.expressions import Q

from apps import cruds
from apps.models import User
from apps.utils.enumerate import SessionTypeEnum


class UserLogic:
    def __init__(self):
        self.crud = cruds.user_crud

    async def get_user_object(self, user_id: int):
        return await self.crud.get_one(id=user_id, is_delete=False)

    async def user_list(self, page: int = 1, page_size: int = 10, name: str = None):
        skip = (page - 1) * page_size
        query_sql = self.crud.model.filter(is_delete=False)
        if name:
            query_sql = query_sql.filter(account__icontains=name)
        count = await query_sql.count()
        if not count:
            data = []
        else:
            data = await query_sql.order_by('-id').offset(skip).limit(page_size).all()
        return count, data


user_logic = UserLogic()
